<template>
  <div class="add-node-btn-box">
    <div class="add-node-btn">
      <span class="line-box"></span>
      <el-popover placement="right-start" v-model="visible">
        <div class="add-node-popover-body">
          <a class="add-node-popover-item approver" @click="addType(4)">
            <div class="item-wrapper">
              <span class="iconfont"></span>
            </div>
            <p>审批人</p>
          </a>
          <a class="add-node-popover-item notifier" @click="addType(5)">
            <div class="item-wrapper">
              <span class="iconfont"></span>
            </div>
            <p>抄送人</p>
          </a>
          <a class="add-node-popover-item condition" @click="addType(2)">
            <div class="item-wrapper">
              <span class="iconfont"></span>
            </div>
            <p>条件分支</p>
          </a>
          <a class="add-node-popover-item approver" @click="addType(6)">
            <div class="item-wrapper">
              <span class="iconfont"></span>
            </div>
            <p>添加会签</p>
          </a>
        </div>
        <button class="btn" type="button" slot="reference">
          <span class="iconfont"></span>
        </button>
      </el-popover>
    </div>
  </div>
</template>
<script>
  import { processGenerateKey } from '../../api/process';
  export default {
    props: ['childNodeP'],
    data() {
      return {
        visible: false,
      };
    },
    methods: {
      async addType(type) {
        // 获取uuid
        let processCode = this.$route.query.key;
        let primaryKeyList = await processGenerateKey({ nodeType: type });
        primaryKeyList = primaryKeyList.rows;
        // let primaryKeyList = [1, 2, 3];
        this.visible = false;
        var data;
        // 更改父节点的子节点
        this.childNodeP.nextNode = primaryKeyList[0].primaryKey;
        // console.log(JSON.parse(JSON.stringify(this.childNodeP)), 'before');

        if (type == 2) {
          data = {
            primaryKey: primaryKeyList[0].primaryKey,
            nodeName: '条件路由',
            nodeType: 2,
            childNode: this.childNodeP.childNode,
            nodeDesc: '', //节点描述
            processCode: processCode,
            organizeCode: '', //组织code
            createUser: '', //创建人信息
            parentNode: this.childNodeP.primaryKey,
            nextNode: this.childNodeP.childNode ? this.childNodeP.childNode.primaryKey : null,
            routerNodeCode: this.childNodeP.routerNodeCode,
            procNodeRuleList: [
              {
                primaryKey: primaryKeyList[1].primaryKey,
                nodeCode: '', //节点node
                nodeName: '条件1',
                conditionValue: '', //条件
                conditionType: '', //条件类型1:表单条件；2:自定义条件
                createUser: '', //创建人信息
                nodeType: 3,
                conditionLevel: 1,
                procNodeConditionList: [],
                childNode: null,
                parentNode: primaryKeyList[0].primaryKey,
                nextNode: '',
                routerNodeCode: primaryKeyList[0].primaryKey,
                processCode: processCode,
              },
              {
                primaryKey: primaryKeyList[2].primaryKey,
                nodeCode: '', //节点node
                nodeName: '条件2',
                conditionValue: '', //条件
                conditionType: '', //条件类型1:表单条件；2:自定义条件
                createUser: '', //创建人信息
                error: true,
                nodeType: 3,
                conditionLevel: 2,
                nextNode: '',
                procNodeConditionList: [],
                childNode: null,
                parentNode: primaryKeyList[0].primaryKey,
                routerNodeCode: primaryKeyList[0].primaryKey,
                processCode: processCode,
              },
            ],
          };
        } else if (type == 4) {
          data = {
            primaryKey: primaryKeyList[0].primaryKey,
            nodeName: '审核人',
            nodeDesc: '', //节点描述
            processCode: processCode,
            organizeCode: '', //组织code
            nodeType: 4, //节点类型
            organizeApproveRule: '', //内部审批规则
            nodeApproveType: 1, //审批节点类型
            createUser: '', //创建人信息
            childNode: this.childNodeP.childNode,
            procNodeConditionList: [],
            procNodeCountersignList: [],
            parentNode: this.childNodeP.primaryKey,
            nextNode: this.childNodeP.childNode ? this.childNodeP.childNode.primaryKey : null,
            routerNodeCode: this.childNodeP.routerNodeCode,
            organizeCode: '',
            organizeName: '',
            fieldKey: '',
            fieldName: '',
            userApiParam: '',
            userApiParamArr: [],
            userApi: '',
            ifGroupNode: '',
            approveLevel: '',
          };
        } else if (type == 5) {
          data = {
            primaryKey: primaryKeyList[0].primaryKey,
            nodeName: '抄送人',
            nodeDesc: '', //节点描述
            processCode: processCode, //流程code
            organizeCode: '', //组织code
            nodeType: 5, //节点类型
            organizeApproveRule: '', //内部审批规则
            nodeApproveType: 1, //审批节点类型
            createUser: '', //创建人信息
            childNode: this.childNodeP,
            procNodeConditionList: [],
            procNodeCountersignList: [],
            nodeUserList: [],
            childNode: this.childNodeP.childNode,
            parentNode: this.childNodeP.primaryKey,
            nextNode: this.childNodeP.childNode ? this.childNodeP.childNode.primaryKey : null,
            routerNodeCode: this.childNodeP.routerNodeCode,
            userApiParam: '',
            userApiParamArr: [],
            userApi: '',
            ifGroupNode: '',
            approveLevel: '',
            organizeCode: '',
            organizeName: '',
            fieldKey: '',
            fieldName: '',
          };
        } else if (type == 6) {
          data = {
            primaryKey: primaryKeyList[0].primaryKey,
            nodeName: '会签路由',
            nodeType: 6,
            childNode: this.childNodeP.childNode,
            nodeDesc: '', //节点描述
            processCode: processCode, //流程code
            organizeCode: '', //组织code
            organizeApproveRule: '', //内部审批规则
            nodeApproveType: '', //审批节点类型
            createUser: '', //创建人信息
            procNodeCountersignList: [],
            parentNode: this.childNodeP.primaryKey,
            nextNode: this.childNodeP.childNode ? this.childNodeP.childNode.primaryKey : null,
            routerNodeCode: this.childNodeP.routerNodeCode,
            userApiParam: '',
            userApiParamArr: [],
            userApi: '',
            ifGroupNode: '',
            approveLevel: '',
            procNodeCountersignList: [
              {
                primaryKey: primaryKeyList[1].primaryKey,
                nodeCode: '', //节点node
                nodeName: '审核人1',
                createUser: '', //创建人信息
                error: true,
                nodeType: 7,
                organizeApproveRule: '', //内部审批规则
                nodeApproveType: 1, //审批节点类型
                procNodeCountersignList: [],
                childNode: null,
                parentNode: primaryKeyList[0].primaryKey,
                nextNode: '',
                routerNodeCode: primaryKeyList[0].primaryKey,
                processCode: processCode,
                organizeCode: '',
                organizeName: '',
                fieldKey: '',
                fieldName: '',
                userApiParam: '',
                userApiParamArr: [],
                userApi: '',
                ifGroupNode: '',
                approveLevel: '',
              },
              {
                primaryKey: primaryKeyList[2].primaryKey,
                nodeCode: '', //节点node
                nodeName: '审核人2',
                createUser: '', //创建人信息
                error: true,
                nodeType: 7,
                organizeApproveRule: '', //内部审批规则
                nodeApproveType: 1, //审批节点类型
                procNodeCountersignList: [],
                childNode: null,
                parentNode: primaryKeyList[0].primaryKey,
                nextNode: '',
                routerNodeCode: primaryKeyList[0].primaryKey,
                processCode: processCode,
                organizeCode: '',
                organizeName: '',
                fieldKey: '',
                fieldName: '',
                userApiParam: '',
                userApiParamArr: [],
                userApi: '',
                ifGroupNode: '',
                approveLevel: '',
              },
            ],
          };
        }
        // 重置自节点的父级key
        if (this.childNodeP.childNode) {
          this.childNodeP.childNode.parentNode = primaryKeyList[0].primaryKey;
        }

        // this.childNodeP.childNode = data
        this.$set(this.childNodeP, 'childNode', data);

        // console.log(JSON.parse(JSON.stringify(this.childNodeP)), 'after');
        this.$emit('update:childNodeP', this.childNodeP);
      },
    },
  };
</script>
<style lang="scss" scoped>
  @import '../../../../common/css/workflow.css';
</style>
